﻿Imports System.Data.SqlClient

Public Class Frm_Idafa_Mowakilin

    Public cli As Client
    Public cltDoss As Dossiers_Clients
    Public advDoss As Dossier_Adversaire
    Dim dossAvo As dossAvoBur
    Dim ListViewItem1 As System.Windows.Forms.ListViewItem



    Public Sub enabledAjouter()
        If Partager.dv.List_client.Count <= 0 Then
            Btn_ajout_mowakil.Enabled = True
        Else
            Btn_ajout_mowakil.Enabled = False
        End If
    End Sub


    Private Sub Btn_parc_marji3ona_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_marji3ona.Click
        Me.Enabled = False
        Partager.frmafect = Me
        Partager.type = Me.Name
        Frm_Rech_Dossier.Show()
    End Sub

    Private Sub Btn_parc_nom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_nom.Click
        Me.Enabled = False
        Partager.frmMoule = New Form
        Partager.frmMoule = Me
        Partager.type_rech_mowakil = "Frm_Ref"
        Frm_Rech_Clients.Show()
    End Sub

    

   
    Public Sub initialCombs()
        If Comb_nom.Items.Count > 0 Then
            Comb_nom.SelectedIndex = -1
        End If
        If Comb_adv.Items.Count > 0 Then
            Comb_adv.SelectedIndex = -1
        End If
        Comb_siffa.SelectedIndex = -1
        cmbCaracEnemie.SelectedIndex = -1
    End Sub
    Private Sub Frm_Idafa_Mowakilin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'dossier avec id
        Partager.remplirDossier(Comb_marji3ona)

        Comb_siffa.SelectedIndex = 0
        Partager.close()
        Try

            '----------------------------------------------------------------
            Partager.remplirClient(Comb_nom)
            initialCombs()
            '----------------------------------------------------------------
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        '------------------------------------------------------------------------
    End Sub

    Private Sub Btn_ajout_mowakil_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout_mowakil.Click

        If Comb_marji3ona.SelectedIndex < 0 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            Try
                If Comb_nom.Text <> "" And Comb_adv.Text <> "" And Comb_siffa.Text <> "" And cmbCaracEnemie.Text <> "" Then
                    If MsgBox("هل تريد فعلا إضافة أطراف هذه الدعوة لهذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة") = MsgBoxResult.Yes Then


                        Partager.deClt = DirectCast(Comb_nom.SelectedItem, DictionaryEntry)
                        Dim ref As Integer = CInt(Partager.deClt.Key)
                        'récupération du ref
                        Partager.deClt = DirectCast(Comb_adv.SelectedItem, DictionaryEntry)
                        Partager.close()

                        cltDoss = New Dossiers_Clients(Partager.dv.id, ref, Comb_siffa.Text)

                        Partager.dv.Ajouter_client(cltDoss)
                        Partager.dv.modifierRefDossier(Partager.deClt.Key)

                        Dim i As Integer = 0

                        For Each adv In Partager.maLstAdver
                            If CInt(Partager.maLstAdver(i).nom) = CType(Comb_adv.SelectedItem, DictionaryEntry).Key Then
                                advDoss = New Dossier_Adversaire(Partager.dv.id, Partager.maLstAdver(i).id, cmbCaracEnemie.Text)
                                Partager.dv.Ajouter_enemi(advDoss)
                                Exit For
                            End If
                        i += 1
                        Next


                        MsgBox("تمت إضافة الأطراف للملف بنجاح", MsgBoxStyle.Information, "نجاح")
                        enabledAjouter()
                    End If
                Else
                MsgBox("لم تقم بملئ الإختيارات كلها", MsgBoxStyle.Information, "نجاح")
                End If
            Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        End If

    End Sub

    Private Sub Comb_marji3ona_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Try

            initialCombs()
            Partager.deClt = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)

            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.id = CInt(Partager.deClt.Key) Then
                    Partager.dv = d
                    Partager.dv.remplir_List_client()
                    Partager.dv.remplir_List_enemi()
                    Exit For
                End If
            Next


            Dim refObj As Reference

            Partager.moi.remplirRef()
            'selectionner le reference du dossier en cours
            Dim reqRef = From refObjReq In Partager.moi.List_Reference
                         Where refObjReq.Ref = Partager.dv.Ref

            If reqRef.Count > 0 Then
                For Each ObjReqRef In reqRef
                    refObj = New Reference(ObjReqRef.Ref, ObjReqRef.dateInscriptionBur)
                    refObj.remplir_lstRefClt()
                Next

                For Each itemCmb As DictionaryEntry In Comb_nom.Items
                    If itemCmb.Key = refObj.lstRefClt(0).id Then
                        Comb_nom.SelectedItem = itemCmb
                        Exit For
                    End If
                Next

                For Each dosClt As Dossiers_Clients In Partager.dv.List_client
                    If dosClt.Id_Clients = refObj.lstRefClt(0).id Then
                        Comb_siffa.Text = dosClt.type
                        Exit For
                    End If
                Next
            End If

            

            enabledAjouter()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_supp_mowakil_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp_mowakil.Click
        If Not Partager.dv Is Nothing Then
            If Partager.dv.List_client.Count > 0 Then
                If MsgBox("هل تريد فعلا حذف هذه الأطراف من هذا الملف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف") = MsgBoxResult.Yes Then
                    Partager.close()
                    Try

                        Partager.dv.suprimmer_client(Partager.dv.id)
                        Partager.dv.suprimmer_enemi(Partager.dv.id)
                        Partager.dv.modifierRefDossier(-1)

                        MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                        enabledAjouter()
                        Comb_nom.SelectedIndex = -1
                        Comb_adv.SelectedIndex = -1
                        Comb_siffa.SelectedIndex = -1
                        cmbCaracEnemie.SelectedIndex = -1
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                End If
            Else
                MsgBox("لم يتم ربط هذا الملف يأي مرجع", MsgBoxStyle.Critical, "خطأ")
            End If
        Else
            MsgBox("اختر الملف أولا", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

   
    

    Private Sub Comb_nom_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Comb_nom.KeyPress
        e.Handled = True
    End Sub





    Private Sub List_mowakilin_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'For Each it As ListViewItem In List_mowakilin.SelectedItems
        '    If it.Selected Then
        '        it.Selected = False
        '    End If
        'Next
    End Sub

    Private Sub Comb_nom_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_nom.SelectedIndexChanged
        If Comb_nom.SelectedIndex >= 0 Then
            Partager.deClt = DirectCast(Comb_nom.SelectedItem, DictionaryEntry)
            Partager.remplir_CombAdvrT(Partager.deClt.Key, Comb_adv)

            If Comb_adv.Items.Count > 0 Then
                Comb_adv.SelectedIndex = -1
            End If
            If Not Partager.dv Is Nothing Then
                Dim varI As Integer

                For Each item As DictionaryEntry In Comb_adv.Items
                    If item.Key = Partager.dv.Ref Then
                        Comb_adv.SelectedItem = item
                        For Each adv In Partager.maLstAdver
                            If CInt(adv.nom) = Partager.dv.Ref Then
                                varI = adv.id
                            End If
                        Next
                        Exit For
                    End If
                Next


                Dim req As IEnumerable(Of String) = From khib As Dossier_Adversaire In Partager.dv.List_enemi
                                          Where khib.Id_Adversaire = varI
                                          Select khib.type

                For Each dosAdv As String In req
                    cmbCaracEnemie.Text = dosAdv
                Next
            End If
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechAdv.Click
        Partager.flag = True
        Me.Enabled = False
        Partager.frmMoule = New Form
        Partager.frmMoule = Me
        Partager.type_rech_adver = "Frm_Idafa_khossoum"
        Partager.moi.remplir_List_enemi()
        Frm_Rech_Adver.Show()
    End Sub

    Private Sub Btn_mohamin_mowakil_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_mohamin_mowakil.Click
        If Comb_marji3ona.SelectedIndex >= 0 Then
            Frm_mohamin_mowakil.MdiParent = Frm_Main
            Me.Enabled = False
            Frm_mohamin_mowakil.Show()
        Else
            MsgBox("اختر الملف أولا", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub btnAfficheAvCltFrm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAfficheAvCltFrm.Click
        If Comb_marji3ona.SelectedIndex >= 0 Then
            Me.Enabled = False
            Frm_Av_Mowakil.Show()
        Else
            MsgBox("اختر الملف أولا", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub btnAfficheAvAdvFrm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAfficheAvAdvFrm.Click
        If Comb_marji3ona.SelectedIndex >= 0 Then
            Me.Enabled = False
            Frm_Av_Enemi.Show()
        Else
            MsgBox("اختر الملف أولا", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub
End Class